/**
  ******************************************************************************
  * @file    main.c 
  * @author  联航精英训练营   allen@unigress.com
  * @version V1.0
  * @date    2013-10-20
  * @brief   本程序是一个用来展示按键外部中断的示例程序。
  ******************************************************************************
  * @attention
  *
  * 本程序所提供的源代码均为本程序作者或由网络收集整理而来，仅供学习和研究使用。
  * 如有侵犯的地方，请及时联系，我们会立即修改。使用本程序源码的用户必须明白，
  * 我们不能保证所提供的源码及其它资源的准确性、安全性和完整性，因此我们将不负责
  * 承担任何直接，间接因使用这些源码对自己和他人造成任何形式的损失或伤害。任何人
  * 在使用本代码时，应注明作者和出处。
  *
  * <h2><center>版权所有(C) 2013 太原联航科技有限公司</center></h2>
  ******************************************************************************
  */  


#include "includes.h"
#include "app_cfg.h"

CirBuff_Def recvBuff;

u8 warnFlag = CMD_WarnData;
u8 isRasp = 0;
u8 showMode = 0;
u8 is_buzzer = 0;
u8 dht_buffer[5] = {0};
u8 temp_MaxV = 40;
u8 temp_MinV = 5;
double temp_value = 0.0;

u8 humi_MaxV = 60;
u8 humi_MinV = 5;
double humi_value = 0.0;

u8 tempStr[5] = {0};
u8 tempStr_Max[3] = {0};
u8 tempStr_Min[3] = {0};

u8 humiStr[5] = {0};
u8 humiStr_Max[3] = {0};
u8 humiStr_Min[3] = {0};

#define START_TASK_PRIO      		10
#define START_STK_SIZE  				64
OS_STK START_TASK_STK[START_STK_SIZE];
void start_task(void *pdata);	
 			   
#define MAIN_TASK_PRIO       			9 
#define MAIN_STK_SIZE  		    		128	
OS_STK MAIN_TASK_STK[MAIN_STK_SIZE];
void MAIN_task(void *pdata);

#define KEY_TASK_PRIO       		  8 
#define KEY_STK_SIZE  		    		128	
OS_STK KEY_TASK_STK[KEY_STK_SIZE];
void KEY_task(void *pdata);

#define DHT_TASK_PRIO       			3 
#define DHT_STK_SIZE  		    		128	
OS_STK DHT_TASK_STK[DHT_STK_SIZE];
void DHT_task(void *pdata);

#define RECV_TASK_PRIO       			2
#define RECV_STK_SIZE  		    		256	
OS_STK RECV_TASK_STK[RECV_STK_SIZE];
void RECV_task(void *pdata);

#define RASP_TASK_PRIO       			6
#define RASP_STK_SIZE  		    		128	
OS_STK RASP_TASK_STK[RASP_STK_SIZE];
void Rasp_task(void *pdata);

// #define SEND_TASK_PRIO       			4 
// #define SEND_STK_SIZE  		    		128	
// OS_STK SEND_TASK_STK[SEND_STK_SIZE];
// void SEND_task(void *pdata);

int main(void)
{
	BSP_Init();		  	   
	OSInit();
	CirBuff_Init(&recvBuff);
 	OSTaskCreate(start_task,(void *)0,
					(OS_STK *)&START_TASK_STK[START_STK_SIZE-1],
					START_TASK_PRIO );//创建起始任务

	OSStart();
}

//开始任务
void start_task(void *pdata)
{
  OS_CPU_SR cpu_sr=0;
	pdata = pdata; 
	OSStatInit();					//初始化统计任务.这里会延时1秒钟左右	
 	OS_ENTER_CRITICAL();			//进入临界区(无法被中断打断) 
	
 	OSTaskCreate(MAIN_task,(void *)0,
					(OS_STK*)&MAIN_TASK_STK[MAIN_STK_SIZE-1],
					MAIN_TASK_PRIO);
	OSTaskCreate(KEY_task,(void *)0,
					(OS_STK*)&KEY_TASK_STK[KEY_STK_SIZE-1],
					KEY_TASK_PRIO);	
	OSTaskCreate(DHT_task,(void *)0,
					(OS_STK*)&DHT_TASK_STK[DHT_STK_SIZE-1],
					DHT_TASK_PRIO);	
	OSTaskCreate(RECV_task,(void *)0,
					(OS_STK*)&RECV_TASK_STK[RECV_STK_SIZE-1],
					RECV_TASK_PRIO);	
	OSTaskCreate(Rasp_task,(void *)0,
					(OS_STK*)&RASP_TASK_STK[RASP_STK_SIZE-1],
					RASP_TASK_PRIO);	
// 	OSTaskCreate(SEND_task,(void *)0,
// 					(OS_STK*)&SEND_TASK_STK[SEND_STK_SIZE-1],
// 					SEND_TASK_PRIO);		
					
	OSTaskSuspend(START_TASK_PRIO);	//挂起起始任务.
	OS_EXIT_CRITICAL();				//退出临界区(可以被中断打断)
}

void RECV_task(void *pdata)
{	 	
	int delay = 0;
	while(OS_TRUE)
	{
		if(delay++ == 50)
		{
			LED_MSGE = !LED_MSGE;
			delay = 0;
		}
		CirBuff_getProtol(&recvBuff);
 		OSTimeDlyHMSM(0,0,0,2);
	};
}

void Rasp_task(void *pdata)
{	 	
	while(OS_TRUE)
	{
		if(isRasp)
		{
			parseWarnDev();
			Mode_ExecGain();
			Temp_ExecReal();
			Temp_ExecMaxV();
			Temp_ExecMinV();
			Humi_ExecReal();
			Humi_ExecMaxV();
			Humi_ExecMinV();
		}
		OSTimeDlyHMSM(0,0,0,500);
	};
}

// void SEND_task(void *pdata)
// {	 	
// 	while(OS_TRUE)
// 	{
// 		sprintf((char *)real_time_data, "%1.2f", value);
// 		sprintf((char *)limit_min_data, "%1.1f", value_min);
// 		sprintf((char *)limit_max_data, "%1.1f", value_max);
// 		
// 		pack_send_data(MODULE_VALUE, PROTOCOL_FULL_CMD, DATA_SIZE, real_time_data);
// 		pack_send_data(MODULE_MIN_VALUE, PROTOCOL_FULL_CMD, DATA_SIZE, limit_min_data);
// 		pack_send_data(MODULE_MAX_VALUE, PROTOCOL_FULL_CMD, DATA_SIZE, limit_max_data);
// 		OSTimeDlyHMSM(0,0,0,200);
// 	};
// }

void DHT_task(void *pdata)
{	 	
	while(OS_TRUE)
	{
      if(dht11_read_data(dht_buffer) == 0)
      {
					LED_DATA = !LED_DATA;
          humi_value = dht_buffer[0] + dht_buffer[1] / 10.0;
          temp_value = dht_buffer[2] + dht_buffer[3] / 10.0;
					if(temp_value != 22.40)
					{
						sprintf((char*)tempStr, "%.1f",temp_value);
						sprintf((char*)humiStr, "%.1f",humi_value);
						if(showMode == 0)
						{
							OLED_ShowString(76,16,tempStr);  
							OLED_ShowString(76,32,humiStr); 
						}else if(showMode == 1)
						{
							OLED_ShowString(76,16,tempStr); 
						}else if(showMode == 2)
						{
							OLED_ShowString(76,16,humiStr); 
						}
						 
						OLED_Refresh_Gram();
					}
      }
		OSTimeDlyHMSM(0,0,0,200);
	};
}

void KEY_task(void *pdata)
{	 
  u8 ts = 0;
	sprintf((char*)tempStr_Max, "%d",temp_MaxV);
	sprintf((char*)tempStr_Min, "%d",temp_MinV);
	sprintf((char*)humiStr_Max, "%d",humi_MaxV);
	sprintf((char*)humiStr_Min, "%d",humi_MinV);
	while(OS_TRUE)
	{
			ts = KEY_Scan(0);		//得到键值
			if (ts > 0)
			{
				BEEP_Tip();   //反转按键值所对应的LED			
			  
				if(ts == KV_ERA)
				{
					is_buzzer = !is_buzzer;
					LED_BEEP = is_buzzer;
					isRasp = is_buzzer;
					if(isRasp)
					{
						Mode_RaspValue();
					}
				}else if(ts == KV_MOD)
				{
					showMode++;
					showMode%=3;
					if(showMode == 0)
					{
						Mode_AllValue();
					}else if(showMode == 1)
					{
						Mode_TempValue();
					}else if(showMode == 2)
					{
						Mode_HumiValue();
					}
				}else if(ts == KV_SUB)
				{
					if(showMode == 1)
					{
						if(temp_MaxV > (temp_MinV+10))
						{
							temp_MaxV-=10;
							sprintf((char*)tempStr_Max, "%d",temp_MaxV);
							OLED_ShowString(36,32,tempStr_Max);
						}
					}else if(showMode == 2)
					{
						if(humi_MaxV > (humi_MinV+10))
						{
							humi_MaxV-=10;
							sprintf((char*)humiStr_Max, "%d",humi_MaxV);
							OLED_ShowString(36,32,humiStr_Max);
						}
					}
					OLED_Refresh_Gram();
				}else if(ts == KV_ADD)
				{
					if(showMode == 1)
					{
						if(temp_MaxV < 90)
						{
							temp_MaxV+=10;
							sprintf((char*)tempStr_Max, "%d",temp_MaxV);
							OLED_ShowString(36,32,tempStr_Max);
						}
					}else if(showMode == 2)
					{
						if(humi_MaxV < 90)
						{
							humi_MaxV+=10;
							sprintf((char*)humiStr_Max, "%d",humi_MaxV);
							OLED_ShowString(36,32,humiStr_Max);
						}
					}
					OLED_Refresh_Gram();
				}
			}
			OSTimeDlyHMSM(0,0,0,20);
	}
}

void MAIN_task(void *pdata)
{	 	
	while(OS_TRUE)
	{
		if(temp_value > temp_MaxV)
		{
			LED_WARN = !LED_WARN;
			if(!is_buzzer) BEEP_Tip();
			WARN_TempValue(WARN_HIG);
		}else if(temp_value < temp_MinV)
		{
			LED_WARN = !LED_WARN;
			if(!is_buzzer) BEEP_Tip();
			WARN_TempValue(WARN_LOW);
		}else if(humi_value > humi_MaxV)
		{
			LED_WARN = !LED_WARN;
			if(!is_buzzer) BEEP_Tip();
			WARN_HumiValue(WARN_HIG);
		}else if(humi_value < humi_MinV)
		{
			LED_WARN = !LED_WARN;
			if(!is_buzzer) BEEP_Tip();
			WARN_HumiValue(WARN_LOW);
		}else
		{
			if(showMode == 0)
			{
				OLED_ShowCHinese(48,48,31,16,1);
				OLED_ShowCHinese(64,48,32,16,1);
				OLED_ShowCHinese(80,48,6,16,1);
				OLED_ShowCHinese(96,48,7,16,1);
				
			}else if(showMode == 1)
			{
				OLED_ShowCHinese(48,48,0,16,1);
				OLED_ShowCHinese(64,48,2,16,1);
				OLED_ShowCHinese(80,48,6,16,1);
				OLED_ShowCHinese(96,48,7,16,1);
			}else if(showMode == 2)
			{
				OLED_ShowCHinese(48,48,1,16,1);
				OLED_ShowCHinese(64,48,2,16,1);
				OLED_ShowCHinese(80,48,6,16,1);
				OLED_ShowCHinese(96,48,7,16,1);
			}
			OLED_Refresh_Gram();
			warnFlag = CMD_WarnData;
			LED_WARN = LED_OFF;
			if(!is_buzzer) BEEP = BEEP_OFF;
		}
		OSTimeDlyHMSM(0,0,0,100);
	};
}

void WARN_TempValue(char cond)
{
	OLED_ShowCHinese(48,48,0,16,1);
	OLED_ShowCHinese(64,48,2,16,1);
	OLED_ShowCHinese(112,48,30,26,1);
	if(cond == WARN_LOW)
	{
		warnFlag = CMD_WarnLowT;
		OLED_ShowCHinese(80,48,25,16,1);
		OLED_ShowCHinese(96,48,27,16,1);
  }else if(cond == WARN_HIG)
	{
		warnFlag = CMD_WarnOvrT;
		OLED_ShowCHinese(80,48,25,16,1);
		OLED_ShowCHinese(96,48,26,16,1);
  }
	OLED_Refresh_Gram();
}
void WARN_HumiValue(char cond)
{
	OLED_ShowCHinese(48,48,1,16,1);
	OLED_ShowCHinese(64,48,2,16,1);
	OLED_ShowCHinese(112,48,30,26,1);
	if(cond == WARN_LOW)
	{
		warnFlag = CMD_WarnLowH;
		OLED_ShowCHinese(80,48,25,16,1);
		OLED_ShowCHinese(96,48,27,16,1);
  }else if(cond == WARN_HIG)
	{
		warnFlag = CMD_WarnOvrH;
		OLED_ShowCHinese(80,48,25,16,1);
		OLED_ShowCHinese(96,48,26,16,1);
  }
	OLED_Refresh_Gram();
}

void Mode_AllValue(void)
{
	OLED_Clear();
	OLED_ShowCHinese(16,0,0,16,1);
	OLED_ShowCHinese(32,0,1,16,1);
	OLED_ShowCHinese(48,0,2,16,1);
	OLED_ShowCHinese(64,0,3,16,1);
	OLED_ShowCHinese(80,0,4,16,1);
	OLED_ShowCHinese(96,0,5,16,1);
	OLED_ShowCHinese(0,16,19,16,1);
	OLED_ShowCHinese(16,16,20,16,1);
	OLED_ShowCHinese(32,16,0,16,1);
	OLED_ShowCHinese(48,16,2,16, 1);
	OLED_ShowString(64,16,(u8*)":");
	OLED_ShowCHinese(112,16,22,16, 1);
	OLED_ShowCHinese(0,32,19,16,1);
	OLED_ShowCHinese(16,32,20,16,1);
	OLED_ShowCHinese(32,32,1,16,1);
	OLED_ShowCHinese(48,32,2,16,1);
	OLED_ShowString(64,32,(u8*)":");
	OLED_ShowCHinese(112,32,21,16, 1);
	OLED_ShowCHinese(0,48,23,16, 1);
	OLED_ShowCHinese(16,48,24,16, 1);
	OLED_ShowString(32,48,(u8*)":");
	OLED_ShowCHinese(48,48,31,16,1);
	OLED_ShowCHinese(64,48,32,16,1);
	OLED_ShowCHinese(80,48,6,16,1);
	OLED_ShowCHinese(96,48,7,16,1);
	OLED_Refresh_Gram();	
}

void Mode_RaspValue(void)
{
	OLED_ShowCHinese(48,48,33,16,1);
	OLED_ShowCHinese(64,48,34,16,1);
	OLED_Refresh_Gram();
}

void Mode_TempValue(void)
{
	OLED_Clear();
	OLED_ShowCHinese(16,0,0,16,1);
	OLED_ShowCHinese(32,0,1,16,1);
	OLED_ShowCHinese(48,0,2,16,1);
	OLED_ShowCHinese(64,0,3,16,1);
	OLED_ShowCHinese(80,0,4,16,1);
	OLED_ShowCHinese(96,0,5,16,1);
	OLED_ShowCHinese(0,16,19,16,1);
	OLED_ShowCHinese(16,16,20,16,1);
	OLED_ShowCHinese(32,16,0,16,1);
	OLED_ShowCHinese(48,16,2,16, 1);
	OLED_ShowString(64,16,(u8*)":");
	OLED_ShowCHinese(112,16,22,16, 1);
	OLED_ShowString(0,32,(u8*)"MAX:");
	OLED_ShowString(36,32,tempStr_Max);
	OLED_ShowString(64,32,(u8*)"MIN:");
	OLED_ShowString(100,32,tempStr_Min);
	OLED_ShowCHinese(0,48,23,16, 1);
	OLED_ShowCHinese(16,48,24,16, 1);
	OLED_ShowString(32,48,(u8*)":");
	OLED_ShowCHinese(48,48,0,16,1);
	OLED_ShowCHinese(64,48,2,16,1);
	OLED_ShowCHinese(80,48,6,16,1);
	OLED_ShowCHinese(96,48,7,16,1);
	OLED_Refresh_Gram();
}


void Mode_HumiValue(void)
{
	OLED_Clear();
	OLED_ShowCHinese(16,0,0,16,1);
	OLED_ShowCHinese(32,0,1,16,1);
	OLED_ShowCHinese(48,0,2,16,1);
	OLED_ShowCHinese(64,0,3,16,1);
	OLED_ShowCHinese(80,0,4,16,1);
	OLED_ShowCHinese(96,0,5,16,1);
	OLED_ShowCHinese(0,16,19,16,1);
	OLED_ShowCHinese(16,16,20,16,1);
	OLED_ShowCHinese(32,16,1,16,1);
	OLED_ShowCHinese(48,16,2,16,1);
	OLED_ShowString(64,16,(u8*)":");
	OLED_ShowCHinese(112,16,21,16, 1);
	OLED_ShowString(0,32,(u8*)"MAX:");
	OLED_ShowString(36,32,humiStr_Max);
	OLED_ShowString(64,32,(u8*)"MIN:");
	OLED_ShowString(100,32,humiStr_Min);
	OLED_ShowCHinese(0,48,23,16, 1);
	OLED_ShowCHinese(16,48,24,16, 1);
	OLED_ShowString(32,48,(u8*)":");
	OLED_ShowCHinese(48,48,1,16,1);
	OLED_ShowCHinese(64,48,2,16,1);
	OLED_ShowCHinese(80,48,6,16,1);
	OLED_ShowCHinese(96,48,7,16,1);
	OLED_Refresh_Gram();
}

/******************* (C) 版权所有 2013 太原联航科技有限公司 *******************/
